package es.decloud.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import es.decloud.dao.AulaDao;
import es.decloud.model.Aula;

/**
 * This class interacts with hibernate session to save/delete and retrieve Aula
 * objects.
 * 
 * @author <a href="mailto:dtercero@gmail.com">David Tercero</a>
 */
@Repository("aulaDao")
public class AulaDaoHibernate extends GenericDaoHibernate<Aula, Long> implements AulaDao {

	/**
	 * Constructor to create a Generics-based version using Role as the entity
	 */
	public AulaDaoHibernate() {
		super(Aula.class);
	}

	/**
	 * {@inheritDoc}
	 */
	public Aula getAulaByNombre(Long idCentro, String nombre) {
		List aulas = getSession().createCriteria(Aula.class).add(Restrictions.eq("centro.id", idCentro))
				.add(Restrictions.eq("nombre", nombre)).list();

		if (aulas.isEmpty()) {
			return null;
		}
		else
			return (Aula) aulas.get(0);
	}

	public List<Aula> getAulas(Long idCentro) {
		Query qry = getSession().createQuery("from Aula a where a.centro.id = :idCentro order by upper(a.nombre)")
				.setLong("idCentro", idCentro);
		return qry.list();
	}

}
